<?php session_start();?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<script LANGUAGE="javascript">
function redirectPageIncomplete()
{
	alert("เกิดข้อผิดพลาดในการเพิ่มข้อมูล");
	location = "../../dialog/dialog_products_add.php";
}

<?php
include '../../../inc/reuse/dbconfig.php';

$id = $_POST['dia_productsId'];
$description = $_POST['dia_description'];
$main_id = $_POST['dia_mainCategory'];
$sub_id = $_POST['dia_subCategory'];
$name = $_POST['dia_productsName'];
$modifier = $_SESSION['uid'];
$modified_datetime = date("YmdHis"); //เก็บวันเดือนปีและเวลาด้วย function date

$nosub = $sub_id;
if($nosub == "") $nosub = 0;

if(isset($description,$name) && $id != ""){
	
	$sql = "UPDATE products
			SET fix_main_products_type_id = '$main_id',	
			fix_sub_products_type_id = $nosub,		
			products_name = '$name',	
			products_description = '$description',	
			modified_master_id = '$modifier',
			modified_date_time = '$modified_datetime' 
			WHERE products_id= $id";
	
			$_SESSION['folder'] = $id;
			$_SESSION['main_id'] = $main_id;
			$_SESSION['sub_id'] = $nosub;
			$_SESSION['edit'] = true;
			echo $sql;
	if(mysql_query($sql)){
		header("Location: ../../dialog/dialog_images_add.php");
	}else{
		echo "redirectPageIncomplete();";
	}
}
else
{
	$sql = "INSERT INTO products (products_id,fix_main_products_type_id,fix_sub_products_type_id,products_name,products_description,inserted_master_id,inserted_date_time,modified_master_id,modified_date_time)
	VALUES (null,$main_id,$nosub,'$name','$description', '$modifier','$modified_datetime', '$modifier', '$modified_datetime')";

	if(mysql_query($sql)){
		$sql = "SELECT MAX(products_id) AS products_id FROM products";
		$sql= 	mysql_query($sql);
		$row = mysql_fetch_array($sql);
		$_SESSION['folder'] = $row['products_id'];
		$_SESSION['main_id'] = $main_id;
		$_SESSION['sub_id'] = $sub_id;
		$_SESSION['edit'] = false;
		mkdir("../../../images/uploads/".$row['products_id'],0777);
		chmod("../../../images/uploads/".$row['products_id'],0777);
		header("Location: ../../dialog/dialog_images_add.php");		
	}else{
		echo "redirectPageIncomplete();";
	}	
}
mysql_close();
?>
</script>
</body>
</html>
